from mbsh.core.db_stats import DbStats

import unittest
import os
import pandas as pd
from mbsh import create_app


class DbStatsTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        app = create_app(os.getenv('FLASK_CONFIG') or 'default')
        app.app_context().push()

    def tearDown(self):
        print('tear down')

    def test_get_report_df(self):
        stats = DbStats("2019-03-04", "2019-03-10")
        df = stats.get_report_df("2019-03-04", "2019-03-10")
        df.to_csv(r'C:\Users\Administrator\Desktop\test.csv', encoding='utf-8')

    def test_stats_stomach_to_df(self):
        stats = DbStats("2019-01-01", "2019-06-15")
        df = stats.case_df
        df = stats.stats_stomach_to_df(df, "放大染色")
        df.to_csv(r'C:\Users\Administrator\Desktop\temp_3.csv', encoding='utf-8')

    def test_stats_intestine_to_df(self):
        stats = DbStats("2019-05-01", "2019-06-01")
        df = stats.case_df
        df = stats.stats_intestine_to_df(df, '全部')
        df.to_csv(r'C:\Users\Administrator\Desktop\temp_4.csv', encoding='utf-8')

    def test_get_month_total(self):
        stats = DbStats("2018-12-01", "2019-06-30")
        df = stats.case_df
        df = stats.get_month_total(df, "息肉治疗")
        df.to_csv(r'C:\Users\Administrator\Desktop\temp_total.csv', encoding='utf-8')

    def test_fill_missing_month(self):
        stats = DbStats("2018-12-01", "2019-06-30")
        data = [{'value': 0.0, 'month': '2019-02'}, {'value': 0.07407407407407407, 'month': '2019-04'},
                {'value': 0.10256410256410256, 'month': '2019-05'}, {'value': 0.0, 'month': '2019-06'}]
        data_li = stats.fill_missing_month("2018-12-01", "2019-06-30", data)
        print(data_li)

    def test_get_three_report_df(self):
        stats = DbStats("2019-01-01", "2019-10-06")
        # 胃类型的数据
        df_s = stats.get_report_df("2019-01-01", "2019-09-29", True)
        # 肠类型的数据
        df_i = stats.get_report_df("2019-01-20", "2019-10-08", True)
        # 要保存的路径以及要保留的字段
        save_path = r'C:\Users\Administrator\Desktop'
        save_path = r'C:\Users\think\Desktop'
        save_fields = ['patient_name', 'check_time', 'operate_type', 'device_id', 'doctor_performance', 'pathology_info']

        stomach_df = pd.DataFrame()
        if len(df_s) > 0:
            stomach_df = df_s[df_s['operate_type'].str.contains('胃镜检查')]

            stomach_df = stomach_df.drop(stomach_df[(
                    stomach_df['operate_type'].str.contains('胃镜检查') & (stomach_df['cost_time'] <= 30) | (
                    stomach_df['cost_time'] >= 1200))].index)
        if len(stomach_df) > 0:
            early_cancer_df = stomach_df[stomach_df['early_cancer'] == 1]
            # 下载早癌的表
            df1 = early_cancer_df
            df1 = df1[save_fields]
            df1.to_csv(os.path.join(save_path,'cancer.csv'), encoding='utf-8', index=False)
            # 下载低级别上皮内瘤变的表
            df2 = stomach_df
            df2 = df2.dropna(subset=['pathology_info'])
            df2 = df2[df2['pathology_info'].str.contains('上皮内瘤变') & df2['pathology_info'].str.contains('低级别')]
            df2 = df2[save_fields]
            df2.to_csv(os.path.join(save_path,'tumour.csv'), encoding='utf-8', index=False)

        intestine_df = pd.DataFrame()
        if len(df_i) > 0:
            intestine_df = df_i[df_i['operate_type'].str.contains('肠镜检查|无法耐受')]
            intestine_df = intestine_df.drop(intestine_df[(
                    intestine_df['operate_type'].str.contains('肠镜检查') & (intestine_df['exit_time'] >= 1200))].index)
        if len(intestine_df) > 0:
            # 下载腺瘤的表
            adenoma_df = intestine_df[intestine_df['adenoma'] == 1]
            df3 = adenoma_df
            df3 = df3[save_fields]
            df3.to_csv(os.path.join(save_path,'adenoma.csv'), encoding='utf-8', index=False)


if __name__ == '__main__':
    unittest.main()
